unit Main;

interface

uses
  System.SysUtils,
  System.Types,
  System.UITypes,
  System.Classes,
  System.Variants,
  FMX.Types,
  FMX.Controls,
  FMX.Forms,
  FMX.Graphics,
  FMX.Dialogs,
  FMX.ScrollBox,
  FMX.Memo,
  FMX.Controls.Presentation,
  FMX.StdCtrls,
  Alcinoe.FMX.ErrorReporting,
  system.Messaging,
  Alcinoe.StringUtils,
  Alcinoe.StringList,
  Alcinoe.FMX.Common,
  Alcinoe.FMX.Facebook.Login,
  Alcinoe.FMX.Facebook.Share,
  Alcinoe.Common,
  FMX.Memo.Types,
  Alcinoe.FMX.Objects;

type
  TForm1 = class(TForm)
    ButtonFacebookLogin: TButton;
    Memo1: TMemo;
    ButtonLogoutFromFacebook: TButton;
    ButtonGetCurrentTokenInfos: TButton;
    ButtonGetCurrentUserInfos: TButton;
    ButtonShowShareLinkDialog: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ButtonFacebookLoginClick(Sender: TObject);
    procedure ButtonGetCurrentTokenInfosClick(Sender: TObject);
    procedure ButtonLogoutFromFacebookClick(Sender: TObject);
    procedure ButtonGetCurrentUserInfosClick(Sender: TObject);
    procedure ButtonShowShareLinkDialogClick(Sender: TObject);
  private
    fFaceBookLogin: TalFacebookLogin;
    fFacebookGraphRequest: TALFacebookGraphRequest;
    procedure onFaceBookLoginCancel;
    procedure onFaceBookLoginError(const aMsg: String);
    procedure onFaceBookLoginSuccess(const aUserID: String; const aToken: String; const AGrantedPermissions: TArray<String>; const ADeniedPermissions: TArray<String>);
    procedure onFaceBookGraphRequestCompleted(const aResponse: string; Const aErrorCode: integer; Const aErrorMsg: String);
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

{*******************************************}
procedure TForm1.FormCreate(Sender: TObject);
begin

  TALErrorReporting.Instance;

  fFaceBookLogin := TalFacebookLogin.Create;
  fFaceBookLogin.onCancel := onFaceBookLoginCancel;
  fFaceBookLogin.onError := onFaceBookLoginError;
  fFaceBookLogin.onSuccess := onFaceBookLoginSuccess;

  fFacebookGraphRequest := TALFacebookGraphRequest.Create;
  fFacebookGraphRequest.onCompleted := onFaceBookGraphRequestCompleted;

end;

{********************************************}
procedure TForm1.FormDestroy(Sender: TObject);
begin
  alFreeAndNil(fFaceBookLogin);
  alFreeAndNil(fFacebookGraphRequest);
end;

{*************************************}
procedure TForm1.onFaceBookLoginCancel;
begin
  allog('FaceBookLogin:onCancel', TalLogType.warn);
  memo1.lines.clear;
  memo1.lines.Add('FacebookLogin:onCancel');
end;

{********************************************************}
procedure TForm1.onFaceBookLoginError(const aMsg: String);
begin
  allog('FaceBookLogin:onError', TalLogType.error);
  memo1.lines.clear;
  memo1.lines.Add(aMsg);
end;

{************************************************************************************************************************************************************************}
procedure TForm1.onFaceBookLoginSuccess(const aUserID: String; const aToken: String; const AGrantedPermissions: TArray<String>; const ADeniedPermissions: TArray<String>);
var S: string;
    i: integer;
begin
  allog('FaceBookLogin:onSuccess', TalLogType.info);
  memo1.lines.clear;

  memo1.lines.Add('UserId: ' + aUserID);
  memo1.lines.Add('-----');
  memo1.lines.Add('Token: ' + aToken);
  memo1.lines.Add('-----');
  S := '';
  for I := Low(AGrantedPermissions) to High(AGrantedPermissions) do
    s := s + AGrantedPermissions[i] + ';';
  memo1.lines.Add('Granted Permissions: ' + S);
  memo1.lines.Add('-----');
  S := '';
  for I := Low(ADeniedPermissions) to High(ADeniedPermissions) do
    s := s + ADeniedPermissions[i] + ';';
  memo1.lines.Add('Denied Permissions: ' + S);
end;

{****************************************************************************************************************************}
procedure TForm1.onFaceBookGraphRequestCompleted(const aResponse: string; Const aErrorCode: integer; Const aErrorMsg: String);
begin
  allog('FaceBookGraphRequest:onCompleted', TalLogType.info);
  memo1.lines.clear;
  memo1.lines.Add('ErrorCode: ' + inttoStr(aErrorCode));
  memo1.lines.Add('ErrorMsg: ' + aErrorMsg);
  memo1.lines.Add('-----');
  memo1.lines.Add(aResponse);
end;

{*********************************************************}
procedure TForm1.ButtonFacebookLoginClick(Sender: TObject);
begin
  fFaceBookLogin.logInWithReadPermissions(['public_profile', 'user_friends', 'email']);
end;

{****************************************************************}
procedure TForm1.ButtonGetCurrentTokenInfosClick(Sender: TObject);
var S: string;
    i: integer;
begin
  memo1.lines.clear;
  memo1.lines.Add('UserId: ' + fFaceBookLogin.CurrentUserId);
  memo1.lines.Add('-----');
  memo1.lines.Add('Token: ' + fFaceBookLogin.CurrentToken);
  memo1.lines.Add('-----');
  S := '';
  for I := Low(fFaceBookLogin.CurrentGrantedPermissions) to High(fFaceBookLogin.CurrentGrantedPermissions) do
    s := s + fFaceBookLogin.CurrentGrantedPermissions[i] + ';';
  memo1.lines.Add('Granted Permissions: ' + S);
  memo1.lines.Add('-----');
  S := '';
  for I := Low(fFaceBookLogin.CurrentDeniedPermissions) to High(fFaceBookLogin.CurrentDeniedPermissions) do
    s := s + fFaceBookLogin.CurrentDeniedPermissions[i] + ';';
  memo1.lines.Add('Denied Permissions: ' + S);
end;

{***************************************************************}
procedure TForm1.ButtonGetCurrentUserInfosClick(Sender: TObject);
begin
  fFacebookGraphRequest.Request('me', ['fields=picture.type(large),id,birthday,cover,email,first_name,gender,languages,last_name,locale,location,name']);
end;

{**************************************************************}
procedure TForm1.ButtonLogoutFromFacebookClick(Sender: TObject);
begin
  fFaceBookLogin.logout;
  memo1.lines.clear;
  memo1.lines.Add('Logged out');
end;

{***************************************************************}
procedure TForm1.ButtonShowShareLinkDialogClick(Sender: TObject);
begin
  TALFacebookShareDialog.ShowShareLinkDialog('https://github.com/MagicFoundation/Alcinoe');
end;

initialization
  {$IFDEF DEBUG}
  ReportMemoryleaksOnSHutdown := True;
  {$ENDIF}
  SetMultiByteConversionCodePage(CP_UTF8);

end.